layui.define(['layer', 'upload'], function(exports){
    var layer = layui.layer;
    var upload = layui.upload;
    
    var uploadComponent = {
        // 图片上传
        image: function(options) {
            var defaultOptions = {
                elem: '#uploadImage',
                url: '/admin/upload/image',
                accept: 'images',
                exts: 'jpg|jpeg|png|gif',
                size: 2048,
                before: function(obj) {
                    layer.load();
                },
                done: function(res) {
                    layer.closeAll('loading');
                    if(res.code == 1) {
                        options.success && options.success(res.data.url);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.closeAll('loading');
                    layer.msg('上传失败', {icon: 2});
                }
            };
            
            upload.render($.extend(defaultOptions, options));
        },
        
        // 视频上传
        video: function(options) {
            var defaultOptions = {
                elem: '#uploadVideo',
                url: '/admin/upload/video',
                accept: 'video',
                exts: 'mp4|avi|mov',
                size: 51200,
                before: function(obj) {
                    layer.load();
                },
                done: function(res) {
                    layer.closeAll('loading');
                    if(res.code == 1) {
                        options.success && options.success(res.data.url);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.closeAll('loading');
                    layer.msg('上传失败', {icon: 2});
                }
            };
            
            upload.render($.extend(defaultOptions, options));
        }
    };
    
    exports('uploadComponent', uploadComponent);
}); 